﻿using JinJun.YShowReception.Web.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JinJun.YShowReception.Core;

namespace JinJun.YShowReception.Web.Attributes
{

    public class ActionAttribute:ActionFilterAttribute  //行为过滤器
    {

        /// <summary>
        /// 调用控制器对应的Action方法之后的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            string controller = filterContext.RouteData.Values["Controller"].ToString();
            string action = filterContext.RouteData.Values["Action"].ToString();
            if (action == "LoginOut")
            {
                return;
            }
            if (action == "Login")
            {
                //获取Ip
                string ip = LocalIP.GetLocalIP();
                //获取系统的日志
                string msg = $"时间:{DateTime.Now},Ip:{ip},已完成控制器:{controller},页面:{action}\r\n";
                //获取文件路径
                string path = "D:\\Action.txt";
                File.AppendAllText(path, msg);
            }
            else
            {
                //获取用户名
                string username = UserInfoContext.userInfoContext.UserInfo.UserName;
                //获取系统的日志
                string msg = $"时间:{DateTime.Now},用户名:{username},已完成控制器:{controller},页面:{action}\r\n";
                //获取文件路径
                string path = "D:\\Action.txt";
                File.AppendAllText(path, msg);
            }
        }


        /// <summary>
        /// 调用控制器对应的Action方法之前的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            string controller = filterContext.RouteData.Values["Controller"].ToString();
            string action = filterContext.RouteData.Values["Action"].ToString();
            if (action == "Login")
            {
                //获取Ip
                string ip = LocalIP.GetLocalIP();
                //获取系统的日志
                string msg = $"时间:{DateTime.Now},Ip:{ip},已完成控制器:{controller},页面:{action}\r\n";
                //获取文件路径
                string path = "D:\\Action.txt";
                File.AppendAllText(path, msg);
            }
            else
            {
                //获取用户名
                string username = UserInfoContext.userInfoContext.UserInfo.UserName;
                //获取系统的日志
                string msg = $"时间:{DateTime.Now},用户名:{username},已完成控制器:{controller},页面:{action}\r\n";
                //获取文件路径
                string path = "D:\\Action.txt";
                File.AppendAllText(path, msg);
            }
        }



        /// <summary>
        /// 调用控制器对应的Action方法之后页面渲染之后的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {

        }

        /// <summary>
        /// 调用控制器对应的Action方法之后页面渲染之前的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {

        }
    }
}